<?php

declare(strict_types=1);

namespace app\api\controller;

use app\common\model\User;
use app\common\model\Pro as ProModel;
use app\common\model\District;
use app\common\model\DistrictPro;
use app\common\model\ProNature;
use app\common\model\ProPlace;
use app\common\model\Company;
use app\common\model\ProCate;
use app\common\model\ProTopic;

use app\common\model\CheckList;
use app\common\model\CheckListDetails;

use think\exception\ValidateException;
use think\response\Json;

class Index extends Base
{

    /**
     * 首页获取列表数据
     */
    public function getList()
    {
        $param = $this->request->get();
        try {
            $dist = District::where(['id' => $param['district_id'], 'is_del' => 0])->find();
            if (empty($dist)) {
                return json(['code' => -1, 'msg' => '区域不存在！']);
            }
            $list = DistrictPro::where(['district_id' => $param['district_id'], 'is_del' => 0])->field('id,title as pro_name,district_id,pro_id,basenum')->order('sort asc')->select();
            //$queslist = CheckListDetails::where(['district_id' => $param['district_id'], 'is_del' => 0, 'types' => 1])->field('id,district_id,check_list_id,topic_id')->group('topic_id')->select();
            $dsum = DistrictPro::where(['district_id' => $param['district_id'], 'is_del' => 0])->order('sort asc')->sum('basenum');
            $counts = 0;
            $sums = 0;
            foreach ($list as $k => $v) {
                $pro = ProModel::where(['id' => $v['pro_id'], 'is_del' => 0])->find();
                if ($pro['marking'] == 1) {
                    $company_id = Company::where(['place_id' => $v['id'],'is_del'=>0])->column('id');
                    $v['count'] = CheckList::whereIn('company_id',$company_id)->where(['pro_id' => $v['pro_id'], 'is_del' => 0, 'district_id' =>  $param['district_id'],'one_status'=>2])->count();
                    if ($pro['types'] == 1) {
                        $feshu = CheckList::whereIn('company_id',$company_id)->where(['pro_id' => $v['pro_id'], 'is_del' => 0, 'district_id' => $param['district_id'],'one_status'=>2])->avg('one_score');
                    } else {
                        $feshu = CheckList::whereIn('company_id',$company_id)->where(['pro_id' => $v['pro_id'], 'is_del' => 0, 'district_id' => $param['district_id'],'one_status'=>2])->sum('one_score');
                    }
                    $sum = round(($feshu * $pro['points_ratio']), 1);
                    $v['sum'] = $sum;
                    $counts += $v['count'];
                    $sums += $sum;
                } else {
                    $v['count'] = '';
                    $v['basenum'] = '';
                    $v['sum'] = -50;
                }
                $list[$k] = $v;
            }
            //print('<pre>');print_r($queslist);die;

            return json(['code' => 1, 'msg' => '获取数据成功！', 'data' => ['list' => $list, 'sum' => round($sums, 1), 'count' => $counts, 'basenum' => $dsum]]);
        } catch (ValidateException $e) {
            return json(['code' => -1, 'msg' => $e->getError()]);
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }
    public function quesList()
    {
        $param = $this->request->get();
        try {
            $dist = District::where(['id' => $param['district_id'], 'is_del' => 0])->find();
            if (empty($dist)) {
                return json(['code' => -1, 'msg' => '区域不存在！']);
            }
            $queslist = CheckListDetails::where(['district_id' => $param['district_id'], 'is_del' => 0, 'types' => 1])->field('id,district_id,check_list_id,topic_id')->group('topic_id')->select()->toArray();


            $num = 20;
            if ($param['page'] == 1) {
                $start = 0;
            } else {
                $start = $num * ($param['page'] - 1);
            }
            foreach ($queslist as $k => $v) {
                $topic = ProTopic::where(['id' => $v['topic_id']])->find();
                // print('<pre>');print_r($topic);die;
                $count = CheckList::where(['pro_id' => $topic['pro_id'], 'is_del' => 0, 'district_id' => $param['district_id'],'one_status'=>2])->count();
                
                $pro = ProModel::where(['id' => $topic['pro_id'], 'is_del' => 0])->find();
                
                $CheckListDetails = CheckListDetails::where(['district_id' => $param['district_id'], 'topic_id' => $v['topic_id'], 'is_del' => 0, 'types' => 1])->count();
                $v['count'] = $CheckListDetails;
                // if ($count < 100) {
                $percentcount = $CheckListDetails;
                // } else {
                if($count!=0){
                    $percent = round(($CheckListDetails / $count * 100), 2);
                }else{
                    $percent=0;
                }
                // }

                $v['percent'] = $percent . '%';
                $v['percents'] = $percent;
                $v['percentcount'] = $percentcount;
                $v['quname'] = $topic['title'];
                $v['proname'] = ProModel::where(['id' => $topic['pro_id'], 'is_del' => 0])->value('title');
                $v['pro_id'] = $pro['id'];
                
                $queslist[$k] = $v;
                $ids = [1, 2, 3, 4];
                if (!in_array($v['pro_id'], $ids)) {
                    unset($queslist[$k]);
                }
                if ($v['percents'] <= 5 || $v['percentcount']<=5) {
                    unset($queslist[$k]);
                }

            }
            if (!empty($queslist)) {
                $list = array_slice($queslist, $start, $num);
                $sort = array_column($list, 'percents');
                array_multisort($sort, SORT_DESC, $list);
                /**数组排序 */
            } else {
                $list = [];
            }

            return json(['code' => 1, 'msg' => '获取数据成功！', 'data' => ['list' => $list]]);
        } catch (ValidateException $e) {
            return json(['code' => -1, 'msg' => $e->getError()]);
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }
}
